package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 备课包资源排序
 * 
 * @author renruihu
 *
 */
@CrossOrigin
@RestController
public class LectureResUpdOrderNumController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/upd/ordernum", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        YxyProfile profile = getProfile(request, response);
        String userId = profile.getId();
        String schoolId = profile.getSchoolId();
        String userNick = profile.getUserNick();
        String schoolName = profile.getSchoolName();
        String packId = reqModel.prePackId;
        YxyLecturePrePack lecturePrePack = yxyLecturePrePackRepository
                .findByIdAndUserIdAndSchoolIdAndIsDeleteNot(packId, userId, schoolId, 1);
        Assert.notNull(lecturePrePack, "备课包不存在");
        List<Res> resList = lecturePrePack.getResources();
        List<ResModel> resModels = reqModel.resList;
        for (ResModel resModel : resModels) {
            String resId = resModel.resId;
            for (Res res : resList) {
                if (StringUtils.equals(res.getResourceId(), resId)) {
                    Integer orderNum = resModel.orderNum;
                    res.setOrderNum(orderNum);
                }
            }
        }
        lecturePrePack.setResources(resList);
        lecturePrePack.setReferencUserId(userId);
        lecturePrePack.setUserNick(userNick);
        lecturePrePack.setSchoolNick(schoolName);
        yxyLecturePrePackRepository.save(lecturePrePack);
        return new YxyApiRespModel("备课包修改排序成功");
    }

    protected static class ResModel {
        public String resId;
        public Integer orderNum;
    }

    protected static class ReqModel {
        public String prePackId;
        public List<ResModel> resList;
    }

}
